library(ggplot2)
library(plotly)
library(dplyr)
library(DT)
library(readxl)
library(lubridate)
datos_policiales <-
  readxl::read_excel("C:/Users/PC/Documents/2022 UCR I/PROCESAMIENTO DE DATOS/Datos Policiales/datos-policiales/estadisticaspoliciales2021.xls")

TABLA DE DATOS DE DELITOS

GRÁFICO DE TIPO DE DELITO

grafico <-
datos_policiales %>%
  count(Delito) %>%
  ggplot(aes(x = reorder(Delito, n), y = n)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  ggtitle("Registro de Delitos") + 
  xlab("Delito") +
  ylab("Cantidad") +
  theme_get()



ggplotly(grafico) %>% config(locale = 'es')

GRÁFICO DELITOS COMETIDOS POR MES

datos_meses <- datos_policiales %>% mutate(fecha_meses = lubridate::month(Fecha))
orden_meses <-c("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre")
delitos_meses <-
  
  datos_meses %>%
  count(fecha_meses) %>%
  ggplot(level = level_order, (aes(x = reorder(orden_meses, fecha_meses), y = n))) +
  geom_bar(stat = "identity") +
  ggtitle("Cantidad de Delitos por mes") +
  xlab("Mes") +
  ylab("Cantidad") +
  coord_flip() + 
  theme_dark()

ggplotly(delitos_meses)

GRÁFICO DE PROPORCIÓN POR GÉNEROS

datos_genero <-
  datos_policiales %>%
  ggplot(aes(x = Delito, fill = Genero)) +
  geom_bar(position = "fill") +
  ggtitle("Proporcion por género") +
  xlab("Proporción") +
  ylab("Género") +
  coord_flip() +
  labs(fill = "Género") +
  theme_classic()

ggplotly(datos_genero)

GRÁFICO DE DELITOS POR CANTONES CENTRALES DEL GAM

datos_canton <-
  
  datos_policiales %>%
  count(Canton) %>%
  filter(Canton == "SAN JOSE" |
           Canton == "HEREDIA" |
           Canton == "ALAJUELA" |
           Canton == "CARTAGO") %>% 
  
  ggplot(aes(x = reorder(Canton, n), y = n)) +
  geom_bar(stat = "identity") +
  ggtitle("Delitos por Cantón") + 
  xlab("Cantón") +
  ylab("Cantidad") +
  theme_classic()

  ggplotly(datos_canton)